package com.book.stat.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.book.business.entity.BookIndent;
import com.book.business.service.BookIndentService;
import com.book.buyout.entity.Prelisting;
import com.book.buyout.service.PrelistingService;
import com.book.common.annotation.ControllerEndpoint;
import com.book.common.controller.BaseController;
import com.book.common.entity.FebsConstant;
import com.book.common.entity.FebsResponse;
import com.book.common.utils.FebsUtil;
import com.book.stat.entity.SellStat;
import com.book.stat.service.SellStatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class SellStatController extends BaseController {
    @Autowired
    PrelistingService prelistingService;
    @Autowired
    BookIndentService bookIndentService;
    @Autowired
    SellStatService sellStatService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "stat/sellStat")
    public String sellStat() {
        return FebsUtil.view("stats/sellStat");
    }

    /*
     * @Description:折线图
     * * @Param: object
     * @Author: Mr.Zhang
     * @Date: 2021/1/12*/

    @GetMapping("sellStat/line/{year}")
    @ResponseBody
    @ControllerEndpoint(operation = "折线图成功渲染", exceptionMessage = "折线图渲染失败")
    public FebsResponse getSellCount(@PathVariable String year) {
        //月份
        String[] month = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};
        //单月报损总数
        int totalIn = 0;
        int totalTwo = 0;
        List<Integer[]> countList = new ArrayList<>();
        Integer[] buyIn = new Integer[12];
        Integer[] sellOut = new Integer[12];
        for (int i = 0; i < month.length; i++) {
//            for (int t = 0; t < type.length; t++) {
            //进货单
            QueryWrapper<Prelisting> queryWrapper = new QueryWrapper<>();
            //已完成订单
            queryWrapper.eq("status", 1);
            //确定年份
            queryWrapper.eq("year", year);
            //确定月份
            queryWrapper.eq("month", month[i]);
            List<Prelisting> prelistingList = prelistingService.list(queryWrapper);
            for (int b = 0; b < prelistingList.size(); b++) {
                totalIn = totalIn + prelistingList.get(b).getNumber();
            }
            buyIn[i] = totalIn;
            totalIn = 0;
            //售货单
            QueryWrapper<BookIndent> bookIndentQueryWrapper = new QueryWrapper<>();
            //已签收
            bookIndentQueryWrapper.eq("flag", 1);
            //确定年份
            bookIndentQueryWrapper.eq("year", year);
            //确定月份
            bookIndentQueryWrapper.eq("month", month[i]);
            List<BookIndent> bookIndentList = bookIndentService.list(bookIndentQueryWrapper);
            for (int b = 0; b < bookIndentList.size(); b++) {
                totalTwo = totalTwo + bookIndentList.get(b).getNumber();
            }
            sellOut[i] = totalTwo;
            totalTwo = 0;
        }
        countList.add(buyIn);
        countList.add(sellOut);
        return new FebsResponse().success().data(countList);
    }


    /*
     * @Description: 饼状图
     * @return: object
     * @Author: Mr.Zhang
     * @Date: 2021/1/12 */

    @GetMapping("sellStat/pctn/{year}")
    @ResponseBody
    @ControllerEndpoint(operation = "饼状图成功渲染", exceptionMessage = "饼状图渲染失败")
    public FebsResponse getSellPctn(@PathVariable String year, SellStat sellStat) {
        return new FebsResponse().success().data(sellStatService.statList(sellStat));
    }

    /**
     * @Description: 购进与售出总数
     * @Param:
     * @return:
     * @Author: Mr.Zhang
     * @Date: 2021/1/15
     */
    @GetMapping("sellStat/total/{year}")
    @ResponseBody
    @ControllerEndpoint(operation = "成功渲染", exceptionMessage = "渲染失败")
    public FebsResponse getSellAndBuyTotal(@PathVariable String year) {
        int in = 0;     //进货量
        int out = 0;    //售出量
        Integer[] total = new Integer[2];
        //同上面，没用purchase类直接拿，是因为没有书籍数量  只有总经费
        QueryWrapper<Prelisting> prelistingQueryWrapper = new QueryWrapper<>();
        prelistingQueryWrapper.eq("status", 1);
        prelistingQueryWrapper.eq("year", year);
        List<Prelisting> prelistingList = prelistingService.list(prelistingQueryWrapper);
        for (int i = 0; i < prelistingList.size(); i++) {
            in = in + prelistingList.get(i).getNumber();
        }
        QueryWrapper<BookIndent> bookIndentQueryWrapper = new QueryWrapper<>();
        bookIndentQueryWrapper.eq("flag", 1);
        bookIndentQueryWrapper.eq("year", year);
        List<BookIndent> bookIndentList = bookIndentService.list(bookIndentQueryWrapper);
        for (int i = 0; i < bookIndentList.size(); i++) {
            out = out + bookIndentList.get(i).getNumber();
        }
        total[0] = in;
        total[1] = out;
        return new FebsResponse().success().data(total);
    }
}
